define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'record/index' + location.search,
                    // add_url: 'record/add',
                    // edit_url: 'record/edit',
                    del_url: 'record/del',
                    multi_url: 'record/multi',
                    import_url: 'record/import',
                    table: 'record',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedNumber: 1,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.name', title: '考生', operate: 'LIKE'},
                        {field: 'exam.title', title: __('Exam.title'), operate: 'LIKE'},
                        {field: 'testpaper.title', title: __('Testpaper.title'), operate: 'LIKE'},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'exam_id', title: __('Exam_id')},
                        // {field: 'testpaper_id', title: __('Testpaper_id')},
                        {field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'minutes', title: __('Minutes')},
                        {field: 'score', title: __('Score'), operate:'BETWEEN'},
                        {field: 'testpaper.grades', title: "试卷总分", operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                       
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:[
                            {
                                name:"detail",
                                text:"答题详情",
                                classname:"btn btn-xs btn-primary btn-dialog",
                                url:"./record/detail"
                                // url:function(v){
                                //     console.log(v)
                                //     return "./record/detail?test="+v.exam.status_text;
                                // }
                            }
                        ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'record/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'record/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'record/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();

            $('.btn-update-record').click(function(){
                console.log('点击了')
                var record_id = $(this).data('record_id')
                var question_id = $(this).data('question_id')
                console.log('record_id: ', record_id);
                console.log('question_id: ', question_id);

                var grade = $(this).prev().val()
                console.log('grade: ', grade);
                
                $.ajax({
                    type: "post",
                    url: "./record/detail",
                    data: {
                        record_id:record_id,
                        question_id:question_id,
                        grade:grade
                    },
                    dataType: "json",
                    success: function (response) {
                        // console.log('response: ', response);
                        if(response.code == 1){
                            layer.msg(response.msg)
                            
                            $('.score b').text(response.data.score)

                            parent.$('.btn-refresh').trigger('click');
                        }
                        else{
                            layer.alert(response.msg)
                        }
                    }
                });

            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
